package com.cheyunkeji.er.view;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.cheyun.netsalev3.R;
import com.cheyunkeji.er.utils.SUtil;

import androidx.annotation.StringRes;


/**
 * 自定义Toast
 *
 * @author wangyajun
 */

public class SToast extends Toast {

    // private not allow constructor
    private SToast(Context context) {
        super(context);
    }


    /**
     * 短 Toast
     *
     * @param textId 字符串资源
     */
    public static void show(@StringRes int textId, int gravity) {
        showAtLooper(SUtil.getString(textId), Toast.LENGTH_SHORT, gravity);
    }

    public static void show(String text, int gravity) {
        showAtLooper(text, Toast.LENGTH_SHORT, gravity);
    }

    /**
     * 短 Toast
     *
     * @param textId 字符串资源
     */
    public static void show(@StringRes int textId) {
        showAtLooper(SUtil.getString(textId), Toast.LENGTH_SHORT, Gravity.BOTTOM);
    }

    /**
     * 短 Toast
     *
     * @param text 展示文字
     */
    public static void show(CharSequence text) {
        showAtLooper(text, Toast.LENGTH_SHORT, Gravity.CENTER);
    }

    @Override
    public void show() {
        super.show();
    }

    /**
     * 长 Toast
     *
     * @param textId 字符串资源
     */
    public static void showLong(@StringRes int textId) {
        showAtLooper(SUtil.getString(textId), Toast.LENGTH_LONG, Gravity.BOTTOM);
    }

    /**
     * 长 Toast
     *
     * @param text 展示文字
     */
    public static void showLong(CharSequence text) {
        showAtLooper(text, Toast.LENGTH_LONG, Gravity.BOTTOM);
    }


    /**
     * 调用线程没有Looper的给加上 Looper
     *
     * @param text
     * @param duration
     * @param gravity
     */
    private static void showAtLooper(final CharSequence text, final int duration, final int gravity) {
        if (Looper.myLooper() != null) {
            createToast(SUtil.appContext, text, duration, gravity)
                    .show();
        } else {
            // run on main looper
            Handler handler = new Handler(Looper.getMainLooper());
            handler.post(new Runnable() {
                @Override
                public void run() {
                    createToast(SUtil.appContext, text, duration, gravity)
                            .show();
                }
            });
        }
    }

    /**
     * 自定义样式的Toast
     *
     * @param textId
     * @return
     */
    public static Toast makeText(int textId) {
        Toast toast;
        if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
            toast = createToast(SUtil.appContext, SUtil.appContext.getString(textId), Toast.LENGTH_SHORT, Gravity.BOTTOM);
            toast.show();
        } else {
            Looper.prepare();
            toast = createToast(SUtil.appContext, SUtil.appContext.getString(textId), Toast.LENGTH_SHORT, Gravity.BOTTOM);
            toast.show();
            Looper.loop();
        }
        return toast;
    }

    /**
     * 自定义样式的Toast
     *
     * @param text
     * @return
     */
    public static Toast makeText(String text) {
        Toast toast;
        if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
            toast = createToast(SUtil.appContext, text, Toast.LENGTH_SHORT, Gravity.BOTTOM);
            toast.show();
        } else {
            Looper.prepare();
            toast = createToast(SUtil.appContext, text, Toast.LENGTH_SHORT, Gravity.BOTTOM);
            toast.show();
            Looper.loop();
        }
        return toast;
    }

    /**
     * 自定义样式的Toast
     *
     * @param context
     * @param textId
     * @param duration
     * @return
     */
    public static Toast makeText(Context context, int textId, int duration) {
        if (null == context)
            return null;
        Toast toast = createToast(context, context.getString(textId), duration, Gravity.BOTTOM);
        toast.show();

        return toast;
    }

    /**
     * 自定义样式的Toast
     *
     * @param context
     * @param text
     * @param duration
     * @return
     */
    public static Toast makeText(Context context, String text, int duration) {
        Toast toast = createToast(context, text, duration, Gravity.BOTTOM);
        toast.show();

        return toast;
    }

    private static Toast createToast(Context context, CharSequence text, int duration, int gravity) {
        LayoutInflater inflate = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View root = inflate.inflate(R.layout.er_t_toast, null);
        View layout = root.findViewById(R.id.layout);
        TextView tv = (TextView) root.findViewById(R.id.tv_text);
        tv.setText(text);

        int w = SUtil.getAvailableW() - (int) (40 * SUtil.density);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
        layout.setLayoutParams(lp);

        Toast toast = XCToast.getInstance().getToast(context);
        //Toast toast = new Toast(context);
        toast.setGravity(gravity, 0, getYoffset());
        toast.setDuration(duration);
        toast.setView(root);

        return toast;
    }

    public static int getYoffset() {
        float v = 0;
        v = (int) (100 * SUtil.density);

        return (int) v;
    }

}
